package lumis.service.tag;

import javax.persistence.NoResultException;
import javax.persistence.Query;

import lumis.content.core.ContentTag;
import lumis.content.core.ContentUtil;
import lumis.content.table.ContentTableSource;
import lumis.doui.processaction.ProcessActionHandler;
import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.util.IResource;

/**
 * Process Action Handler for Tags, verifies if a similar tag already exists and provides a normalizedName field value.
 * 
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class TagProcessActionHandler extends ProcessActionHandler<ContentTableSource> 
{
	IResource resource;
	
	@Override
	protected IResource getResource() throws PortalException 
	{
		if(resource == null)
		{
			resource = super.getResource();
			resource.addResourcePath("lumis/service/tag/strings/strings");
		}
		
		return resource;
	}
	
	public void processAction() throws PortalException 
	{
		String tagName = getParameter("name", String.class);
		
		if(tagName.contains(ContentTag.TAG_STRING_SEPARATOR))
			throw new PortalException("STR_TAG_NAME_WITH_RESERVED_WORD;"+tagName+";"+ContentTag.TAG_STRING_SEPARATOR, getResource());
		
		if(!ContentUtil.isValidTagName(tagName))
			throw new PortalException("STR_TAG_NAME_WITH_RESERVED_WORD;"+tagName+";{", getResource());
		
		String normalizedTagName = ContentUtil.normalizeTagName(tagName);
		
		String itemId = getParameter("id", String.class);
		if(tagAlreadyExists(itemId, normalizedTagName, douiContext.getRequest().getServiceInstanceConfig().getId()))
			throw new PortalException("STR_TAG_ALREADY_EXISTS;"+tagName, getResource());
		
		douiContext.getRequest().setAttribute("normalizedName", normalizedTagName);
		
		addDefaultResponse();
	}
	
	/**
	 * Verifies if a tag already exists in specific tag repository and is a different item of the given itemId.
	 * 
	 * 
	 * @param itemId
	 * @param normalizedTagName
	 * @param serviceInstanceId
	 * @return
	 * @since 6.0.0
	 */
	private boolean tagAlreadyExists(String itemId, String normalizedTagName, String serviceInstanceId) 
	{
		boolean alreadyExists;
		try
		{
			// builds the query
			StringBuffer querySB = new StringBuffer(250);
			querySB.append("select c from ContentTag t inner join t.contentVersion tcv, ContentLocale cl inner join cl.content c ");
			querySB.append("where tcv = cl.activeVersion and c.serviceInstanceId = :serviceInstanceId and t.normalizedName = :normalizedName ");
			// include the itemId if is not null
			if(itemId != null)
				querySB.append("and tcv.itemId != :itemId");
			
			Query query = ManagerFactory.getEntityManager().createQuery(querySB.toString())
				.setParameter("normalizedName", normalizedTagName)
				.setParameter("serviceInstanceId", serviceInstanceId);
			
			// include the itemId parameter if is not null
			if(itemId != null)
				query.setParameter("itemId", itemId);
			
			query.getSingleResult();
		
			alreadyExists = true;
		}
		catch (NoResultException e) 
		{
			alreadyExists = false;
		}
		
		return alreadyExists;
	}
}
